package list;

public class Exam2_14 {
	public static void main(String[] args){
		Node2_14 head=new Node2_14();
		Node2_14 p= head;
		for(int i =0;i<10;i++){
			int m = (int)(Math.random()*100);
			Node2_14 node2_14=new Node2_14();
			node2_14.data = m%10;
			System.out.print(m%10+" ");
			p.next=node2_14;
			p=p.next;
			head.size++;
		}
		
		int position = Exam2_14.nodeDelete(head, 5);
		System.out.println("element 5 deleted position id "+ position);
		
		Exam2_14.converse(head);
		System.out.println();
		System.out.println(head.size);
		System.out.println("converse");
		
		Node2_14 pre = head;
		Node2_14 q,curr = head.next;
		head.next =null;
		while(curr != null){
			q=curr;
			System.out.print(curr.data+" ");
			curr=q.next;			
			pre.next=curr;			
		}
		System.out.println();
		System.out.println(head.size);
	}
	
	public static int nodeDelete(Node2_14 head,int x){
		Node2_14 pre = head;
		Node2_14 curr,q;
		int position = -1;
		curr = head.next;
		while(curr != null){
			position++;
			if(curr.data == x){
				q=curr;
				curr=q.next;
				pre.next=curr;
				head.size--;
				return position;
			}
			else{
				pre=curr;
				curr=curr.next;
			}
		}
		return -1;
	}
	
	public static int nodeMoreDelete(Node2_14 head,int x){
		Node2_14 pre = head;
		Node2_14 curr,q;
		int tag=0;
		curr = head.next;
		while(curr != null){
			if(curr.data == x){
				q=curr;
				curr=q.next;
				pre.next=curr;
				tag++;
				head.size--;
			}
			else{
				pre=curr;
				curr=curr.next;
			}
		}
		return tag;
	}
	
	public static void converse(Node2_14 head){
		Node2_14 p,q;
		p = head.next;
		head.next =null;
		while(p !=null){
			System.out.print(p.data+" ");
			q=p;
			p=p.next;
			q.next=head.next;
			head.next=q;
		}
	}
}


class Node2_14 {
	public int data;
	public Node2_14 next;
	int size;
}